<?php
/**
 * @author Demianchuk Dmitro
 * @email d.demianchuk@gmail.com
 * @date 05.01.12
 * @version 1.0
 * @package adminArea.components.ATableEditor
 */
class SeoInput extends EditorComponent
{
	public function show(){
		$title = '';
		$keywords = '';
		$description = '';
		$sef = '';
		if($this->itemId!=0){
			$title = $this->current->seo->title->string;
			$keywords = $this->current->seo->keywords->string;
			$description = $this->current->seo->description->string;
			$sef = $this->current->seo->sef;
		}
		return <<<SQL
<table cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td style="width:1%;">Чпу: </td>
		<td><input type="text" value="{$sef}" name="_form[seo][sef]" /></td>
	</tr>
	<tr>
		<td style="width:1%;">Title: </td>
		<td><input type="text" value="{$title}" name="_form[seo][title]" /></td>
	</tr>
	<tr>
		<td>Keywords: </td>
		<td><input type="text" value="{$keywords}" name="_form[seo][keywords]" /></td>
	</tr>
	<tr>
		<td>Description: </td>
		<td><input type="text" value="{$description}" name="_form[seo][description]" /></td>
	</tr>
</table>
SQL;
	}

	public function save($model){
		if(ATableEditor::isNewRecord()){
			$model->seo = new Seo();
			$model->seo->title = new Text();
			$model->seo->keywords = new Text();
			$model->seo->description = new Text();
		}
		$model->seo->sef = $_POST['_form']['seo']['sef'];
		$model->seo->title->string = $_POST['_form']['seo']['title'];
		$model->seo->keywords->string = $_POST['_form']['seo']['keywords'];
		$model->seo->description->string = $_POST['_form']['seo']['description'];
		$model->seo->title->save();
		$model->seo->keywords->save();
		$model->seo->description->save();
		$model->seo->title_id = $model->seo->title->id;
		$model->seo->keywords_id = $model->seo->keywords->id;
		$model->seo->description_id = $model->seo->description->id;
		if(!$model->seo->save()){
			AMessage::add($model->getErrors(),'warning');
		}
		$model->seo_id = $model->seo->id;
		return $model;
	}

	public function isNull(){
		return false;
	}
}
